package Toepen.LoungeFacade;


import java.io.File;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Leest en schrijft de configuratiebestanden. Singleton.
 * @author Bart Piggen
 */
public class Configuration {
    private static String fName = "toepen.config";
    private static Configuration instance;

    private String dbLocation;
    private String dbUsername;
    private String dbPassword;
    private String loungeLocation;
    private String loungePort;

    private Configuration(boolean includeServerSettings) throws java.lang.Exception
    {
        File f=new File(fName);
        if(!f.exists())
            this.createProperties(includeServerSettings);

        readProperties();
    }

    /**
     * Singleton access client
     * @return Singleton instance van Configuration voor de client
     * @throws java.lang.Exception
     */
    public static Configuration getClientInstance() throws java.lang.Exception
    {
        if(instance==null)
            instance=new Configuration(false);

        return(instance);
    }

    /**
     * Singleton access server
     * @return Singleton instance van Configuration voor de server
     * @throws java.lang.Exception
     */
    public static Configuration getServerInstance() throws java.lang.Exception
    {
        if(instance==null)
            instance=new Configuration(true);

        return(instance);
    }

    /**
     * @return IP-adres van de database
     */
    public String getDBLocation()
    {
        return(dbLocation);
    }

    /**
     * @return IP-adres van de lounge
     */
    public String getLoungeLocation()
    {
        return(loungeLocation);
    }

    /**
     * @return IP-poort waarop de lounge zich bevindt
     */
    public String getLoungePort()
    {
        return(loungePort);
    }

    /**
     * @return Login voor de database
     */
    public String getDBUsername()
    {
        return(dbUsername);
    }

    /**
     * @return password voor de database
     */
    public String getDBPassword()
    {
        return(dbPassword);
    }
    
    private void createProperties(boolean includeServerSettings) throws java.lang.Exception
    {
        java.util.Properties properties = new java.util.Properties();
        properties.setProperty("LoungeLocation", "rmi://127.0.0.1:2084/lounge");
        if(includeServerSettings)
        {
            properties.setProperty("LoungePort", "2084");
            properties.setProperty("DBLocation", "jdbc:mysql://85.17.19.174/toepen");
            properties.setProperty("DBUser", "toepserver");
            properties.setProperty("DBPassword", "IsoButane");
        }

        java.io.FileOutputStream configFile = new java.io.FileOutputStream(fName);

        properties.storeToXML(configFile, "Lounge properties");
        configFile.close();
    }
    
    private void readProperties() throws java.lang.Exception
    {
        java.util.Properties properties = new java.util.Properties();
        java.io.FileInputStream iFile = new java.io.FileInputStream(fName);
        properties.loadFromXML(iFile);
        dbLocation=properties.getProperty("DBLocation");
        dbUsername=properties.getProperty("DBUser");
        dbPassword=properties.getProperty("DBPassword");
        loungeLocation=properties.getProperty("LoungeLocation");
        loungePort=properties.getProperty("LoungePort");
    }
}
